//
//  CYDefaultRecordPics.m
//  CreditAddressBook
//
//  Created by Lee on 15/8/6.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYDefaultRecordPics.h"
#import "CYCommon.h"
#import "CYURL.h"
#import "UIImageView+WebCache.h"

@implementation CYDefaultRecordPics {
    UIView *_photoView;
}

- (NSMutableArray *)compactImageUrls {
    if (!_compactImageUrls) {
        _compactImageUrls = [NSMutableArray array];
    }
    return _compactImageUrls;
}

- (NSMutableArray *)certificateUrls {
    if (!_certificateUrls) {
        _certificateUrls = [NSMutableArray array];
    }
    return _certificateUrls;
}

- (NSMutableArray *)picidCardZImageUrls{
    if (!_picidCardZImageUrls) {
        _picidCardZImageUrls = [NSMutableArray array];
    }
    return  _picidCardZImageUrls;
}

- (NSMutableArray *)picidCardFImageUrls{
    if (!_picidCardFImageUrls) {
        _picidCardFImageUrls = [NSMutableArray array];
    }
    return  _picidCardFImageUrls;
}

- (NSMutableArray *)picpersonImageUrls{
    if (!_picpersonImageUrls) {
        _picpersonImageUrls = [NSMutableArray array];
    }
    return  _picpersonImageUrls;
}

- (NSMutableArray *)piccontractImageUrls{
    if (!_piccontractImageUrls) {
        _piccontractImageUrls = [NSMutableArray array];
    }
    return  _piccontractImageUrls;
}

- (NSMutableArray *)picotherImageUrls{
    if (!_picotherImageUrls) {
        _picotherImageUrls = [NSMutableArray array];
    }
    return  _picotherImageUrls;
}

- (NSMutableArray *)otherImageUrls {
    if (!_otherImageUrls) {
        _otherImageUrls = [NSMutableArray array];
    }
    return _otherImageUrls;
}
/**
 *  图片展示view    picidCardZImageUrls picidCardFImageUrls picpersonImageUrls piccontractImageUrls picotherImageUrls
 */
- (UIView *)picAddressListViewCompletion:(void(^)(CGFloat viewH))completion {
    if (!self.compactImageUrls.count && !self.certificateUrls.count && !self.otherImageUrls.count && !self.picidCardZImageUrls.count && !_picidCardFImageUrls.count && !_picpersonImageUrls.count && !_piccontractImageUrls.count && !_picotherImageUrls) return nil;
    NSMutableArray *urls = [NSMutableArray array];
    if (self.compactImageUrls.count) {
        [urls addObjectsFromArray:self.compactImageUrls];
    }
    if (self.certificateUrls.count) {
        [urls addObjectsFromArray:self.certificateUrls];
    }
    if (self.otherImageUrls.count) {
        [urls addObjectsFromArray:self.otherImageUrls];
    }
    if (self.picidCardZImageUrls.count) {
        [urls addObjectsFromArray:self.picidCardZImageUrls];
    }
    if (self.picidCardFImageUrls.count) {
        [urls addObjectsFromArray:self.picidCardFImageUrls];
    }
    if (self.picpersonImageUrls.count) {
        [urls addObjectsFromArray:self.picpersonImageUrls];
    }
    if (self.piccontractImageUrls.count) {
        [urls addObjectsFromArray:self.piccontractImageUrls];
    }
    if (self.picotherImageUrls.count) {
        [urls addObjectsFromArray:self.picotherImageUrls];
    }
    if (urls.count) {
        return [self imageViewWithUrls:urls completion:completion];
    }
    
    return nil;
}




- (UIView *)imageViewWithUrls:(NSArray *)urls completion:(void(^)(CGFloat viewH))completion {
    if (_photoView)  return _photoView;
    if (urls && urls.count) {
        NSArray *compactImageUrls = urls;
        CGFloat imageW = CYScreeSize.width - 48;
        CGFloat imageH = 40;
        __block CGFloat viewH = 0;
        UIView *view = [[UIView alloc] initForAutoLayout];
        view.width = imageW;
        view.height = imageH;
        //        view.backgroundColor = [UIColor blueColor];
        NSMutableArray *images = [NSMutableArray array];
        for (int i = 0; i < compactImageUrls.count; i++) {
            NSString *path = compactImageUrls[i];
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.width = imageW;
            imageView.height = imageH;
            //            imageView.backgroundColor = [UIColor redColor];
            
            [view addSubview:imageView];
            if (compactImageUrls.count == 1) {
                [imageView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
            } else if (i == 0) {
                [imageView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeBottom];
            } else if (i == compactImageUrls.count - 1) {
                [imageView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
                [imageView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:images.lastObject withOffset:8];
            } else {
                [imageView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:images.lastObject withOffset:8];
                [imageView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:view];
                [imageView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:view];
                viewH += 8;
            }
            
            NSLayoutConstraint *a =[imageView autoSetDimension:ALDimensionHeight toSize:imageH];
            [imageView sd_setImageWithURL:CYURLWithService(path) placeholderImage:[UIImage imageNamed:@"putao_icon_quick_replace_s"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                CGFloat imH = image.size.height;
                CGFloat imW = image.size.width;
                a.constant = imageW * (imH/imW);
                viewH += a.constant;
                if (completion) {
                    if (i == compactImageUrls.count - 1) {
                        completion(viewH);
                    }
                }
            }];
            [images addObject:imageView];
        }
        _photoView = view;
        return view;
    }
    return nil;
}
@end
